<?php

class CommentsController extends ModuleController{
    public $per_page = 10;
    private $current_page = 0;
    public function printCommentsList() { 
        $this->model = new CommentsModel();
        if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>=0)
        {
             $this->current_page = $_GET['page'];
        }
        
        $page = (isset($_GET['rewritequery'])?$_GET['rewritequery']:'base');
        $from = $this->current_page*$this->per_page;
        $to = $this->per_page;
        $comments_data = $this->model->getAllCommentsOfPagePager($page, $from, $to);
        $num = $this->model->getNumOfCommentsOnPage($page);
        

        $html = $this->view->getComments_list(array('comments'=>$comments_data));
        
        $pager = $this->view->getPager(array('total'=>ceil($num/$this->per_page)));
        $this->output($pager);
        $this->output($html);
        $this->output($pager);
    }
    public function printCommentsInputBlock() { 
        $this->model = new CommentsModel();
        if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>=0)
        {
             $this->current_page = $_GET['page'];
        }
        
        $params['data'] = $this->getFormManager()->loadPOSTData('addcomment');
        $params['errors'] = $this->getFormManager()->getErrorsForFields('addcomment');
        $form_params = array(
            'body' => $this->view->getcomments_inputbox($params),
            'action' => 'addcomment',
            'buttons'=>'<input type="submit" value="Добавить комментарий">'
        );
        $html = View::form_ex($form_params);
        $this->output($html);
    }
    public function validate_post_addcomment(&$data, &$scope, &$manager) {
        if(!isset($data['subject']) || strlen($data['subject'])<5)
        {         
            $manager->addErorrMessageForField("subject", "Ошибочка вышла");
            return VALIDATION_FAILED;
        }
        return VALIDATION_ACCEPTED;
    }
    public function submit_post_addcomment($data, $scope, $manager) {
        Event::fireEvent('message', array('message' => "Комментарий добавлен"));
        $model = new CommentsModel();
        $model->addComment(array('page'=>(isset($_GET['rewritequery'])?$_GET['rewritequery']:'base'), 'from'=>$this->user->user_id, 'message'=>$data['subject']));
    }  
}

?>
